package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.redis.service.IStrategyStratisVoRedisService;
import cn.wolfcode.wolf2w.repository.StrategyCommentRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {
    @Autowired
    private StrategyCommentRepository strategyCommentRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IStrategyStratisVoRedisService strategyStratisVoRedisService;

    @Override
    public void save(StrategyComment strategyComment) {
        strategyComment.setId(null);
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public void update(StrategyComment strategyComment) {
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public void delete(String id) {
        strategyCommentRepository.deleteById(id);
    }

    @Override
    public StrategyComment get(String id) {
        return strategyCommentRepository.findById(id).orElse(null);
    }

    @Override
    public List<StrategyComment> listAll() {
        return strategyCommentRepository.findAll();
    }

    @Override
    public void addComment(StrategyComment strategyComment) {
        strategyComment.setCreateTime(new Date());

        // 评论数 + 1
        strategyStratisVoRedisService.replynumIncrease(strategyComment.getStrategyId(),1);
        this.save(strategyComment);
    }

    @Override
    public Page<StrategyComment> query(StrategyCommentQuery qo) {
        Query query = new Query();
        if(StringUtils.hasLength(qo.getStrategyId())){
            query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        }
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(), Sort.Direction.DESC,"createTime");
        return DBHelper.query(mongoTemplate,query,StrategyComment.class,pageable);
    }

    @Override
    public void commentThumb(String cid, String id) {
        // 获取集合
        StrategyComment strategyComment = this.get(cid);
        List<String> thumbuplist = strategyComment.getThumbuplist();

        int thumbupnum = strategyComment.getThumbupnum();
        // 判断用户是否存在集合中
        if (thumbuplist.contains(id)){
            // 存在，取消操作
            strategyComment.setThumbupnum(thumbupnum-1);
            thumbuplist.remove(id);
        }else {
            // 不存在，点赞
            strategyComment.setThumbupnum(thumbupnum+1);
            thumbuplist.add(id);
        }
        // 更新
        this.update(strategyComment);
    }
}
